package io.github.jesse205.deskclock.lite.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.text.DateFormat;
import java.util.TimeZone;

import io.github.jesse205.deskclock.lite.BuildConfig;
import io.github.jesse205.deskclock.lite.utils.TimeUpdateTimer;

public class TimeUpdateReceiver extends BroadcastReceiver {
    private static final String TAG = "TimeUpdateReceiver";
    private final TimeUpdateTimer timer;
    private final DateFormat[] dateFormats;

    public TimeUpdateReceiver(TimeUpdateTimer timeUpdateTimer, DateFormat[] dateFormats) {
        this.timer = timeUpdateTimer;
        this.dateFormats = dateFormats;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "onReceive: " + intent);
        }
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
            TimeZone timeZone = TimeZone.getDefault();
            for (DateFormat dateFormat : dateFormats) {
                dateFormat.setTimeZone(timeZone);
            }
        }
        if (timer.isStarted()) {
            Log.i(TAG, "Timer restarted due to action: " + intent.getAction());
            timer.restart();
        }
    }
}
